package jrobutt.room.level;

import jrobutt.*;

import java.util.*;
import java.awt.event.*;

public class GuyInputMover implements KeyboardResponder
{
    private static final Map<Integer, Vector2> my_keys;
    
    static
    {
        my_keys = new HashMap<Integer, Vector2>();
        
        my_keys.put(KeyEvent.VK_D, new Vector2( 1.0f, 0.0f)); // right
        my_keys.put(KeyEvent.VK_A, new Vector2(-1.0f, 0.0f)); // left
        my_keys.put(KeyEvent.VK_S, new Vector2( 0.0f, 1.0f)); // left
        my_keys.put(KeyEvent.VK_W, new Vector2( 0.0f,-1.0f)); // left
    }
    
    public Guy subject;
    private Vector2 current_input_vecdir;
    
    public GuyInputMover (Guy subject)
    {
        this.subject = subject;
        current_input_vecdir = new Vector2(Vector2.ZERO);
    }
    
    private void apply_to_subject ()
    {
        if (current_input_vecdir.equals(Vector2.ZERO))
        {
            subject.stop_moving();
        }
        else
        {
            Vector2 temp_vecdir = current_input_vecdir.cloned();
            temp_vecdir.normalize();
            subject.move_in_direction(temp_vecdir);
        }
    }
    
    public void key_pressed (int key)
    {
        Vector2 direction = my_keys.get(key);
        if (direction == null) return;
        
        current_input_vecdir.add(direction);
        
        apply_to_subject();
    }
    public void key_released (int key)
    {
        Vector2 direction = my_keys.get(key);
        if (direction == null) return;
        
        current_input_vecdir.subtract(direction);
        
        apply_to_subject();
    }
}
